package com.servlet.project.controller;

import com.google.gson.Gson;
import com.servlet.project.entity.Administrator;
import com.servlet.project.entity.Member;
import com.servlet.project.service.AdministratorService;
import com.servlet.project.service.MemberService;
import com.servlet.project.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "LoginServlet",urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type=request.getParameter("type");
        String method=request.getParameter("method");
        request.setCharacterEncoding("utf-8");
        if(method.equals("login")){
            if(type.equals("1")){//管理员登录
                AdministratorService administratorService=new AdministratorService();
                //这里类型要设置未text/json
                response.setContentType("text/json;charset=utf-8");
                String memberName=request.getParameter("memberName");
                String memberPassword=request.getParameter("memberPassword");
                List<Object> objects=new ArrayList<>();
                objects.add(memberName);
                objects.add(memberPassword);
                Administrator administrator=administratorService.login(objects);
                //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
                PrintWriter writer = response.getWriter();
                if(administrator!=null){//登录成功
                    request.getSession().setAttribute("administrator",administrator);
                    writer.write(new Gson().toJson(Result.reOk("登录成功")));
                }else{//账号密码错误
                    writer.write(new Gson().toJson(Result.reError("账号密码错误")));
                }
            } else{//会员登录
                MemberService memberService=new MemberService();
                //这里类型要设置未text/json
                response.setContentType("text/json;charset=utf-8");
                String memberName=request.getParameter("memberName");
                String memberPassword=request.getParameter("memberPassword");
                List<Object> objects=new ArrayList<>();
                objects.add(memberName);
                objects.add(memberPassword);
                Member member=memberService.login(objects);
                //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
                PrintWriter writer = response.getWriter();
                if(member!=null){//登录成功
                    request.getSession().setAttribute("member",member);
                    writer.write(new Gson().toJson(Result.reOk("登录成功")));
                }else{//账号密码错误
                    writer.write(new Gson().toJson(Result.reError("账号密码错误")));
                }
            }
        }else if(method.equals("register")){
            if(type.equals("1")){//管理员注册
                AdministratorService administratorService=new AdministratorService();
                //这里类型要设置未text/json
                response.setContentType("text/json;charset=utf-8");
                String memberName=request.getParameter("membername");
                String memberPassword=request.getParameter("memberpassword");
                String memberBirthdate=request.getParameter("memberbirthdate");
                String memberSex=request.getParameter("membersex");
                String memberEmail=request.getParameter("memberemail");
                List<Object> objects=new ArrayList<>();
                objects.add(memberName);
                objects.add(memberPassword);
                objects.add(memberBirthdate);
                objects.add(memberSex);
                objects.add(memberEmail);
                int count=administratorService.add(objects);
                //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
                PrintWriter writer = response.getWriter();
                if(count==1){//注册成功
                    writer.write(new Gson().toJson(Result.reOk("注册成功")));
                }else{//注册失败
                    writer.write(new Gson().toJson(Result.reError("注册失败")));
                }
            } else{//会员注册
                MemberService service=new MemberService();
                response.setContentType("text/json;charset=utf-8");
                String memberName=request.getParameter("membername");
                String memberPassword=request.getParameter("memberpassword");
                String memberBirthdate=request.getParameter("memberbirthdate");
                String memberSex=request.getParameter("membersex");
                String memberEmail=request.getParameter("memberemail");
                List<Object> objects=new ArrayList<>();
                objects.add(memberName);
                objects.add(memberPassword);
                objects.add(memberBirthdate);
                objects.add(memberSex);
                objects.add(memberEmail);
                int count=service.add(objects);
                //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
                PrintWriter writer = response.getWriter();
                if(count==1){//注册成功
                    writer.write(new Gson().toJson(Result.reOk("注册成功")));
                }else{//注册失败
                    writer.write(new Gson().toJson(Result.reError("注册失败")));
                }
            }
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doPost(request,response);
    }


}
